package com.zm.demo.core.leetcode;

public class L33 {
    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int start = 0;
        int end = nums.length - 1;
        int mid = (start + end) / 2;

        while (start <= end) {
            String s = "";

            if (target == nums[start]) {
                return start;
            } else if (target == nums[mid]) {
                return mid;
            } else if (target == nums[end]) {
                return end;
            }

            if (nums[start] < nums[mid]){//前半段有序
                if (nums[start] < target && target < nums[mid]) {//前半段
                    end = mid - 1;
                } else {//后半段
                    start = mid + 1;
                }
            }else {//后半段有序
                if (nums[end] > target && target > nums[mid]) {//后半段
                    start = mid + 1;
                } else {//前半段
                    end = mid - 1;
                }
            }
        }
        return -1;
    }
}
